/**
 * 
 */
package com.hexacta.hrs.repositories.impl;

import java.util.Collection;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.vectrics.user.Person;

import com.hexacta.hrs.repositories.PersonRepository;

/**
 * 
 * @author mtestasecca
 */
public class PersonRecordObjectRepository extends HibernateRecordObjectRepository<Person> implements PersonRepository {

    /** {@inheritDoc} */
    public PersonRecordObjectRepository(final Class<Person> entityClass) {
        super(entityClass);
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    @Override
    public Collection<Person> matching(final Person criteria) {
        Criteria crit = getSession().createCriteria(Person.class);

        // Filtro por nombre
        if (criteria.getFirstName() != null) {
            crit.add(Restrictions.ilike("firstName", criteria.getFirstName()));
        }

        // Filtro por apellido
        if (criteria.getLastName() != null) {
            crit.add(Restrictions.ilike("lastName", criteria.getLastName()));
        }

        if (criteria.getUser() != null) {
            crit.add(Restrictions.ilike("user", criteria.getUser()));
        }

        // Se devuelven los filtros.
        return crit.list();
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.hexacta.hrs.repositories.PersonRepository#getByEmail(java.lang.String
     * )
     */
    @Override
    public Person getByEmail(final String email) {
        Criteria crit = getSession().createCriteria(Person.class);

        crit.createCriteria("contactInfo").add(Restrictions.eq("email", email));

        List<Person> persons = crit.list();

        if (!persons.isEmpty())
            return persons.get(0);

        return null;
    }
}